
Log.likelihood.fun=function(
    all.data,
    params,
    Nsample,
    maxCounter,
    stepSize,
    congruency,
    bandwidth,
    getSimData) {
  
  allSimData=getSimData(
    N = Nsample,
    params = params,
    maxCounter = maxCounter,
    stepSize = stepSize,
    congruency = congruency,
    use.table = use.table,
    n.table.options = n.table.options
  )
  PDF=list()
  for (use.resp in 1:2) {                              # 根据两个 resp 分别计算 pdf
    sampvec=allSimData$rt[allSimData$resp==use.resp]   # 模拟 rt
    data=all.data$rt[all.data$resp==use.resp]          # 数据 rt
    
    if (length(data)==0) {
      PDF[[use.resp]]=NULL
    } else if (length(sampvec)==0) {
      PDF[[use.resp]]=rep(0,length(data))
    } else {
      
      m=min(data)-3*bandwidth;                         # 最小 rt
      M=max(data)+3*bandwidth;                         # 最大 rt
      
      if ( (min(sampvec)>M) | (max(sampvec)<m) ) {
        PDF[[use.resp]] = rep(0,length(data))
      } else {
        d = density(sampvec,bw=bandwidth,from=m,to=M,n=1024)  # simu-data kernel
        d$y[d$y<0] = 0
        d$y = d$y*length(sampvec)/Nsample
        out = numeric(length(data))
        ok = (data>d$x[1]) & (data<d$x[length(d$x)])
        out[ok] = approx(d$x,d$y,data[ok])$y                  # 两个 data 的 kernel
        out[is.na(out) | !is.finite(out)] = 0
        PDF[[use.resp]] = out
      }
    }
  }
  # resp=-1 是 na 反应，直接求和后用来做 binomial
  list(PDF,sum(allSimData$resp==-1))                         
}
